package hierarchy

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/ANN/server/model/hierarchy/request"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type FdArticleVideoApi struct {
}

var fdArticleVideoService = service.ServiceGroupApp.HierarchyServiceGroup.FdArticleVideoService

var UserService = service.ServiceGroupApp.UserServiceGroup.UserService

// CreateFdArticleVideo 創建FdArticleVideo
// @Tags FdArticleVideo
// @Summary 創建FdArticleVideo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleVideo true "創建FdArticleVideo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /fdArticleVideo/createFdArticleVideo [post]
func (fdArticleVideoApi *FdArticleVideoApi) CreateFdArticleVideo(c *gin.Context) {
	var fdArticleVideo hierarchy.FdArticleVideo
	err := c.ShouldBindJSON(&fdArticleVideo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleVideoService.CreateFdArticleVideo(&fdArticleVideo); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeleteFdArticleVideo 删除FdArticleVideo
// @Tags FdArticleVideo
// @Summary 删除FdArticleVideo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleVideo true "删除FdArticleVideo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /fdArticleVideo/deleteFdArticleVideo [delete]
func (fdArticleVideoApi *FdArticleVideoApi) DeleteFdArticleVideo(c *gin.Context) {
	var fdArticleVideo hierarchy.FdArticleVideo
	err := c.ShouldBindJSON(&fdArticleVideo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleVideoService.DeleteFdArticleVideo(fdArticleVideo); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFdArticleVideoByIds 批量删除FdArticleVideo
// @Tags FdArticleVideo
// @Summary 批量删除FdArticleVideo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除FdArticleVideo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /fdArticleVideo/deleteFdArticleVideoByIds [delete]
func (fdArticleVideoApi *FdArticleVideoApi) DeleteFdArticleVideoByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleVideoService.DeleteFdArticleVideoByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFdArticleVideo 更新FdArticleVideo
// @Tags FdArticleVideo
// @Summary 更新FdArticleVideo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleVideo true "更新FdArticleVideo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /fdArticleVideo/updateFdArticleVideo [put]
func (fdArticleVideoApi *FdArticleVideoApi) UpdateFdArticleVideo(c *gin.Context) {
	var fdArticleVideo hierarchy.FdArticleVideo
	err := c.ShouldBindJSON(&fdArticleVideo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleVideoService.UpdateFdArticleVideo(fdArticleVideo); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindFdArticleVideo 用id査詢FdArticleVideo
// @Tags FdArticleVideo
// @Summary 用id査詢FdArticleVideo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.FdArticleVideo true "用id査詢FdArticleVideo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdArticleVideo/findFdArticleVideo [get]
func (fdArticleVideoApi *FdArticleVideoApi) FindFdArticleVideo(c *gin.Context) {
	var fdArticleVideo hierarchy.FdArticleVideo
	err := c.ShouldBindQuery(&fdArticleVideo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if refdArticleVideo, err := fdArticleVideoService.GetFdArticleVideo(fdArticleVideo.ID, userID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"refdArticleVideo": refdArticleVideo}, c)
	}
}

// GetFdArticleVideoList 分页獲取FdArticleVideo列表
// @Tags FdArticleVideo
// @Summary 分页獲取FdArticleVideo列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.FdArticleVideoSearch true "分页獲取FdArticleVideo列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /fdArticleVideo/getFdArticleVideoList [get]
func (fdArticleVideoApi *FdArticleVideoApi) GetFdArticleVideoList(c *gin.Context) {
	var pageInfo hierarchyReq.FdArticleVideoSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := fdArticleVideoService.GetFdArticleVideoInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// FdArticleCollect 喜欢FdArticle
// @Tags FdArticleVideo
// @Summary 用id査詢FdArticleVideo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdArticleAudio/like [get]
func (fdArticleAudioApi *FdArticleVideoApi) FdArticleLike(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleVideoService.FdArticleLike(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		err = UserService.AddPoints(userID, 1, 1)
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}

// FdArticleCollect 收藏Video
// @Tags FdArticleVideo
// @Summary 收藏Video
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdArticleVideo/collect [get]
func (fdArticleVideoApi *FdArticleVideoApi) FdArticleCollect(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleVideoService.FdArticleCollect(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}
